package com.example.safeassistan.main;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends BaseActivity {
    private static final int REQUEST_CODE = 101;
    private int from = 0;
    private NoScrollViewPager vpContent;
    private BottomNavigationView navigation;
    private MainPageController pageController;
    boolean isExit = false;//退出程序标识
    @Override
    protected void init(Bundle savedInstanceState) { setAndroidNativeLightStatusBar(true); }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_main;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        checkPermission();
        from = getIntent().getIntExtra("from", 0);
        vpContent = findViewById(R.id.vpContent);
        pageController = new MainPageController(MainActivity.this, vpContent);
        navigation = findViewById(R.id.navigation);
        //修改为原图片颜色
        navigation.setItemIconTintList(null);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        switch (from) {
            case 0:
                navigation.setSelectedItemId(R.id.navigation_home);
                break;
            case 1:
                navigation.setSelectedItemId(R.id.navigation_risk);
                break;
            case 2:
                navigation.setSelectedItemId(R.id.navigation_warnning);
                break;
            case 3:
                navigation.setSelectedItemId(R.id.navigation_train);
                break;
            case 4:
                navigation.setSelectedItemId(R.id.navigation_my);
                break;
        }

        pageController.ChangePage(from);
        setAndroidNativeLightStatusBar(true);
    }


    /****权限**/
    private void checkPermission(){
        try{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED
                        ||PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED
                        ||PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)!= PermissionChecker.PERMISSION_GRANTED) {
                    // 不相等 请求授权
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.REQUEST_INSTALL_PACKAGES
                            ,Manifest.permission.CAMERA
                    },REQUEST_CODE);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d("aaa", "onRequestPermissionsResult: " +
                            "权限申请成功");
                    for (int i : grantResults) {
                        Log.d("aaa", "onRequestPermissionsResult: " + i);
                    }
                } else {
                    Toast.makeText(MainActivity.this, "您已拒绝申请权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }
    @Override
    protected void initData() { }

    @Override
    public int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    /**
     * from = 0，首页
     * @param context
     * @param i
     */
    public static void actionStart(Context context, int i) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("from", i);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 底部导航栏点击事件
     */
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    from = 0;
                    pageController.ChangePage(0);
                    setAndroidNativeLightStatusBar(true);
                    return true;
                case R.id.navigation_risk:
                    from = 1;
                    pageController.ChangePage(1);
                    setAndroidNativeLightStatusBar(true);
                    return true;
                case R.id.navigation_warnning:
                        from = 2;
                        pageController.ChangePage(2);
                        setAndroidNativeLightStatusBar(true);
                    return true;
                case R.id.navigation_train:
                    from = 3;
                    pageController.ChangePage(3);
                    setAndroidNativeLightStatusBar(true);
                    return true;
                case R.id.navigation_my:
                    from = 4;
                    pageController.ChangePage(4);
                    setAndroidNativeLightStatusBar(true);
                    return true;
            }
            return false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            switch (from) {
                case 0:
                    exitByDoubleClick();
                    break;
                case 1:
                case 2:
                case 3:
                case 4:
                    from = 0;
                    navigation.setSelectedItemId(R.id.navigation_home);
                    setAndroidNativeLightStatusBar(true);
                    break;
            }
        }
        return false;
    }

    //双击返回退出
    private void exitByDoubleClick() {
        Timer tExit = null;
        if (!isExit) {
            isExit = true;
            Toast.makeText(this, "再返回一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;//取消退出
                }
            }, 2000);// 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

}